<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域-->
    <el-card class="box-card">
      <!--搜索区域-->
      <el-row :gutter="18">
        <el-col :span="4">
          <el-button type="primary"  @click="addCatDialogShow">添加分类</el-button>
        </el-col>
      </el-row>
      <!--用户列表区域-->
      <tree-table :data="cateList" :columns="columns" :selection-type="false" :expand-type="false" show-index  index-text="#" border :show-row-hover="false">
        //是否有效
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        //排序模板
        <template slot="order" slot-scope="scope">
          <el-tag type="primary" size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success" size="mini" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>
        //操作模板
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 7]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--添加分类对话框 添加分类-->
    <el-dialog title="添加分类"  :visible.sync="addCatDialogVisible" width="50%" @close="dialogClose">
      <!--内容区域-->
      <el-form ref="addCateformref"  label-width="100px" :model="catfrmInfo" :rules="catRules" >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="catfrmInfo.cat_name" ></el-input>
        </el-form-item>
        <el-form-item label="父级分类:" widh="100%">
          <el-cascader expand-trigger="hover"  clearable change-on-select
            v-model="selectedopt"
            :options="parentCateList"
            :props="cascadeProps"   @change="parentCateChange"
            >
          </el-cascader>
        </el-form-item>
      </el-form>
      <!--底部区域-->
      <span slot="footer" class="dialog-footer">
             <el-button @click="addCatDialogVisible = false">取 消</el-button>
             <el-button type="primary" @click="addCatDialogConfirm">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    name: 'Cate',
     data: function () {
      return {
        cateList:[],
        //为table指定列
        columns: [
          {label : '分类名称',
          prop: 'cat_name'},
          {
            label: '是否有效',
            //模板列
            type: 'template',
            //模板名称
            template: 'isok'
          },
          {
            label:'排序',
            type: 'template',
            template: 'order'
          },
          {
            label:'操作',
            type: 'template',
            template: 'opt'
          }
        ],
        queryInfo:{
          type:3,
          pagenum:  1,
          pagesize: 5
        },
        total:0,
        addCatDialogVisible:false,
        catfrmInfo:{
          cat_name:'',
          cat_pid:0,
          cat_level:0
        },
        catRules:{
          cat_name: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
        },
        parentCateList:[],
        cascadeProps:{
          value:'cat_id',
          label:'cat_name',
          children:'children',
          checkStrictly: true
        },
        //选中的父级分类
        selectedopt: []
      }
    },
    created () {
      console.log('created')
      this.getCateList();
    },
    methods:{
      //获取商品分类数据
    async getCateList(){
        var data=await this.$axios.get("categories",{params: this.queryInfo});
        var res=data.data;
        console.log("catelist=",res.data);
        if(res.meta.status!=200){
          return this.$message.error("获取商品分类数据失败!");
        }
        this.cateList=res.data.result;
        this.total=res.data.total;
      },
    handleSizeChange(newsize){

      this.queryInfo.pagesize=newsize;
      console.log(this.queryInfo);
      this.getCateList();
    },

    handleCurrentChange(pagenum){
      this.queryInfo.pagenum=pagenum;
      console.log(this.queryInfo);
      this.getCateList();
    },
    dialogClose(){
      this.$refs.addCateformref.resetFields();
    },
      //点击显示添加分类窗口
      addCatDialogShow(){
      this.addCatDialogVisible=true;
      this.getParfentCateList();
      this.catfrmInfo.cat_level=0;
      this.catfrmInfo.cat_pid=0;
      this.catfrmInfo.cat_name='';
      },
      //获取父级分类数据
      async getParfentCateList(){
      var data=await this.$axios.get("categories",{params: {type:2}});
      var res=data.data;
      if(res.meta.status!=200){return this.$message.error("获取商品分类数据失败!")}
      this.parentCateList=res.data;
        console.log(this.parentCateList);
      },
      //父级改变后的事件
      parentCateChange(){
        console.log(this.selectedopt);
        this.catfrmInfo.cat_pid=this.selectedopt[this.selectedopt.length-1];
        this.catfrmInfo.cat_level=this.selectedopt.length;
      },
      //分类窗口确定按键事件处理
      async addCatDialogConfirm(){
        console.log(this.catfrmInfo)
        var data= await this.$axios.post('categories',this.catfrmInfo);
       var res=data.data;
       if(res.meta.status!=201){ return this.$message.error('添加分类失败!');}
       this.$message.success('添加分类成功！');
       this.getCateList();
       this.addCatDialogVisible=false;

    }
    }

  }


</script>

<style scoped lang="less">
.el-button{
  margin: 5px;
}
  .el-cascader{
    width: 100%;
  }
</style>
